from django.db import models

from apps.users.models import BaseModel
from apps.organizations.models import Teacher
from apps.organizations.models import CourseOrg

# Create your models here.
# 课程难度选择
DEGREE_CHOICES = (
    ("elementary-level", "初级"),
    ("middle-level", "中级"),
    ("high-level", "高级"),

)


# 课程信息表
class Course(BaseModel):
    # 授课教师
    teacher = models.ForeignKey(Teacher, verbose_name="授课教师", on_delete=models.CASCADE)
    # 授课机构
    course_org = models.ForeignKey(CourseOrg, verbose_name="授课机构", on_delete=models.CASCADE, null=True, blank=True)
    # 课程名
    name = models.CharField(verbose_name="课程名", max_length=50)
    # 课程简介
    desc = models.CharField(verbose_name="课程简介", max_length=300)
    # 课程时长
    learn_times = models.IntegerField(verbose_name="课程时长（分钟数）", default=0)
    # 课程难度
    degree = models.CharField(verbose_name="课程难度", choices=DEGREE_CHOICES, max_length=16)
    # 学习（学生）人数
    students = models.IntegerField(verbose_name="学习人数", default=0)
    # 收藏人数
    fav_nums = models.IntegerField(verbose_name="收藏人数", default=0)
    # 点击数
    click_nums = models.IntegerField(verbose_name="点击数", default=0)
    # 课程公告
    notice = models.CharField(verbose_name="课程公告", max_length=300, default="")
    # 课程类别
    category = models.CharField(verbose_name="课程类别", max_length=20, default=u"后端开发")
    # 课程标签
    tag = models.CharField(verbose_name="课程标签", max_length=10, default="")
    # 课程须知
    you_need_know = models.CharField(verbose_name="课程须知", max_length=300, default="")
    # 老师的话
    teacher_tell = models.CharField(verbose_name="老师说", max_length=300, default="")
    # 课程详情
    detail = models.TextField(verbose_name="课程详情")
    # 课程封面
    image = models.ImageField(verbose_name="课程封面", upload_to="courses/%Y%m", max_length=100)
    # 是否经典课程
    is_classic = models.BooleanField(verbose_name="经典课程", default=False)

    # 配置信息
    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 课程章节表
class Lesson(BaseModel):
    # 外键； on_delete：表示对应数据被删除后，当前数据应如何处理
    course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.CASCADE)
    # 章节名
    name = models.CharField(verbose_name=u"章节名", max_length=100)
    # 章节时长
    learn_times = models.IntegerField(verbose_name=u"章节时长（分钟数）", default=0)

    # 配置信息
    class Meta:
        verbose_name = "课程章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 视频表
class Video(BaseModel):
    # 外键
    lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.CASCADE)
    # 视频名称
    name = models.CharField(verbose_name=u"章节视频名", max_length=50)
    # 视频时长
    learn_times = models.IntegerField(verbose_name=u"视频时长（分钟数）", default=0)
    # 播放地址
    url = models.CharField(verbose_name=u"视频访问地址", max_length=200)

    # 配置信息
    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 课程资源
class CourseResources(BaseModel):
    # 外键
    course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.CASCADE)
    # 课程资源名称
    name = models.CharField(verbose_name=u"课程资源名称", max_length=100)
    # 课程资源地址
    file = models.FileField(verbose_name="课程资源地址", upload_to="course/resources/%Y%m", max_length=200)

    # 配置信息
    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 课程标签
class CourseTag(BaseModel):
    # 外键
    course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.CASCADE)
    # 课程资源名称
    tag = models.CharField(verbose_name="课程标签", max_length=100)

    # 配置信息
    class Meta:
        verbose_name = "课程标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag
